home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / N-P / PixelFlipper src / Sources / PixelFlippercdev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-04  |  9.1 KB  |  376 lines  |  [TEXT/KAHL]

  1. /*
  2.     PixelFlippercdev.c
  3.     written in Think C 4.0 by Chris Sanchez
  4.     Copyright ©1990 Chris Sanchez, All Rights Reserved
  5. */
  6.  
  7. #include "PixelFlipper.h"
  8. #include "FlipperUtils.h"
  9. #include <cdev.h>
  10.  
  11. /*  PixelFlipper cdev subclass  */
  12. struct PixelFlipper:cdev{
  13.  
  14.     /*    Instance variables    */
  15.     PREFPtr        fPrefs, fOldPrefs;                                        /*  Preferences record pointer. original and real                                                          */
  16.     Boolean        fPrefsCreat, fAbout;                                     /*     prefs were created, doing about                                                                                     */
  17.     short            fVRef, fLoaded, fScrolled, fAmount;        /*    working directory of the system folder, was INIT loaded, amount scrolled    */
  18.     PicHandle    fAboutPic;                                                        /*    handle to the about picture                                                                                                */
  19.     RgnHandle    fClipRgn;                                                            /*    this is a clipping region, so we dont have to keep on cretine new ones        */
  20.     Cursor        fCursor;
  21.  
  22.     /*    PixelFlipper Methods    */            
  23.     void    Init();
  24.     void    Close();
  25.     void    Update();
  26.     void    ItemHit();
  27.     void    Idle();
  28.     void    DoCursor();
  29.     void     Toggle(short whichKey, ControlHandle theItem);
  30.     void     SetDCtl(short which, char value);
  31.     void    DrawAbout();
  32.     void     SetPrefsCtl();
  33.     void     ClearPrefsCtl();
  34. };
  35.  
  36.  
  37. Boolean Runnable()
  38. {
  39.     return(TRUE);
  40. }
  41.  
  42.  
  43. cdev *New()
  44. {
  45.     return new(PixelFlipper);
  46. }
  47.  
  48. void PixelFlipper::Init()
  49. {    SysEnvRec        s;
  50.     PREFPtr            tPrefs;
  51.     CursHandle    cursH;
  52.     Rect                bounds;
  53.     short                theType;
  54.     Handle            theItem;
  55.     DialogPtr        theDlog;
  56.             
  57.     SysEnvirons (curSysEnvVers,&s);                        /*     Get some system info.                                */
  58.     fVRef = s.sysVRefNum;
  59.     fPrefsCreat = fAbout = FALSE;
  60.     fLoaded = fScrolled = 0;
  61.     fClipRgn = NewRgn();
  62.     fAboutPic = GetPicture(kCdevAbout);
  63.     MoveHHi(fAboutPic);
  64.     theDlog = dp;
  65.     GetDItem(theDlog, kB1 + lastItem, &theType, &theItem, &bounds);
  66.     fAmount = (**fAboutPic).picFrame.bottom - (**fAboutPic).picFrame.top - (bounds.bottom - bounds.top);
  67.     cursH = GetCursor(kCursID);
  68.     fCursor = **cursH;
  69.     ReleaseResource(cursH);
  70.  
  71.     fPrefs = (PREFPtr)FindInitData();
  72.  
  73.     if (fPrefs == NIL){                                        /* can't find the fPrefs pointer from the init */
  74.         fLoaded = kNotLoaded;
  75.         if ((fPrefs = (PREFPtr)NewPtrClear(sizeof(PREFRec))) == NIL)
  76.             return;
  77.         tPrefs = fPrefs;
  78.         if (!Get_Set_Prefs(tPrefs, s.sysVRefNum, getting))                /*     Try to get the preferences                    */
  79.             return;
  80.     }
  81.     if ((fOldPrefs = (PREFPtr)NewPtrClear(sizeof(PREFRec))) == NIL)
  82.         fOldPrefs = fPrefs;    /* just make it point to the old data */
  83.     else{        /* we were able to create the fOldPrefs rec */
  84.         fPrefsCreat = TRUE;
  85.         *fOldPrefs = *fPrefs; /* copy the data to the old fPrefs */
  86.     }
  87.     SetPrefsCtl();
  88.     Update();
  89.     inherited::Init();
  90. }
  91.  
  92. void PixelFlipper::Close()
  93. {    PREFPtr                tempPrefs;
  94.     short                    theRef;
  95.     OSErr                    err;
  96.     PicHandle            pic;
  97.         
  98.     if (fPrefsCreat){
  99.         *fPrefs = *fOldPrefs;
  100.         tempPrefs = fPrefs;
  101.         theRef = fVRef;
  102.         if (!Get_Set_Prefs(tempPrefs, theRef , setting))    /* we couldn't save the preferences */
  103.             err = CheckError(fnfErr, noErr, kSaving);
  104.         tempPrefs = fOldPrefs;
  105.         DisposPtr(tempPrefs);
  106.     }
  107.     if (fLoaded == kNotLoaded){
  108.         tempPrefs = fPrefs;
  109.         DisposPtr(tempPrefs);
  110.     }
  111.     if (fAboutPic){
  112.         pic = fAboutPic;
  113.         KillPicture(pic);
  114.     }
  115.     inherited::Close();
  116. }
  117.  
  118.  
  119. void PixelFlipper::ItemHit(item)
  120. int item;
  121. /*    ->    nothing is passed.  This dialog will handle the users preferences for hotKey,
  122.                  displaying the start icon, and making the changes permanent.
  123.         <-    nothing is returned.
  124. */
  125. {    Rect            tempRect;
  126.     short            DType, theRefNum, itemHit, temp;
  127.     Handle        DItem;
  128.     DialogPtr    theDlog;
  129.  
  130.     theDlog = dp;
  131.     GetDItem(theDlog, item + lastItem, &DType, &DItem, &tempRect);
  132.     switch (item){
  133.         case kOption:
  134.             Toggle(optionKey, (ControlHandle)DItem);
  135.             break;
  136.         
  137.         case kShift:
  138.             Toggle(shiftKey, (ControlHandle)DItem);
  139.             break;
  140.         
  141.         case kCommand:
  142.             Toggle(cmdKey, (ControlHandle)DItem);
  143.             break;
  144.         
  145.         case kControl:
  146.             Toggle(controlKey, (ControlHandle)DItem);
  147.             break;
  148.         
  149.         case kCapsLock:
  150.             Toggle(alphaLock, (ControlHandle)DItem);
  151.             break;
  152.         
  153.         case kShowIcon:
  154.             temp = GetCtlValue((ControlHandle)DItem);
  155.             SetCtlValue((ControlHandle)DItem, ((temp + 1) & 1));
  156.             fOldPrefs->showIcon = (temp + 1) & 1;
  157.             break;
  158.         
  159.         case kChanges:
  160.             temp = GetCtlValue((ControlHandle)DItem);
  161.             SetCtlValue((ControlHandle)DItem, ((temp + 1) & 1));
  162.             fOldPrefs->isPerm = (temp + 1) & 1;
  163.             break;
  164.             
  165.         case kRevert:
  166.             SetPrefsCtl();
  167.             *fOldPrefs =*fPrefs;
  168.             break;
  169.                         
  170.         case kActive:
  171.             SetDCtl(kActive, TRUE);
  172.             SetDCtl(kInactive, FALSE);            
  173.             fOldPrefs->active = TRUE;
  174.             break;
  175.             
  176.         case kInactive:
  177.             SetDCtl(kActive, FALSE);
  178.             SetDCtl(kInactive, TRUE);
  179.             fOldPrefs->active = FALSE;
  180.             break;
  181.             
  182.         case kAbout:
  183.         case kCICN:
  184.             fAbout = !fAbout;
  185.  
  186.     }
  187. }
  188.  
  189. void PixelFlipper::Idle()
  190. {    long tix;
  191.  
  192.     if (fAbout){
  193.         if (fScrolled > fAmount){
  194.             fScrolled = 0;
  195.             Delay(75, &tix);
  196.         }else
  197.             fScrolled += 1;
  198.         DrawAbout();
  199.     }
  200. }
  201.  
  202. void PixelFlipper::DoCursor()
  203. {    Cursor            the_Curs;
  204.  
  205.     the_Curs = fCursor;
  206.     SetCursor(&the_Curs);
  207. }
  208.  
  209. void PixelFlipper::Toggle(short whichKey, ControlHandle theItem)
  210. /*    ->    theKey, theItem.  The key is the modifiers flag, and theItem is a 
  211.                 ControlHandle that we are toggeling on and off.  For our purposes
  212.                 at least 1 item in the dialog has to be on for us to be active.
  213.         <-    nothing is returned.
  214. */
  215. {    short             value;
  216.  
  217.     value = GetCtlValue(theItem);
  218.     SetCtlValue(theItem, ((value + 1) & 1));
  219.     if ((fOldPrefs->hotKey ^= whichKey)==0){    /* have to have at least one selected */
  220.         SetCtlValue(theItem, 1);            
  221.         fOldPrefs->hotKey ^= whichKey;
  222.     }
  223. }
  224.  
  225. void PixelFlipper::SetDCtl(short which, char value)
  226. /*    ->    dlog, which, value.  dlog is the dialog on which the controls are stored.  which is
  227.                 the control to set.  value is the state to set the control to.
  228.         <-    nothing is returned.
  229. */
  230. {    Rect                theRect;
  231.     short                theType;
  232.     Handle            theItem;
  233.     DialogPtr        theDlog;
  234.     
  235.     theDlog = dp;
  236.     GetDItem(theDlog, which + lastItem, &theType, &theItem, &theRect);
  237.     SetCtlValue((ControlHandle)theItem, value);
  238. }
  239.  
  240. void PixelFlipper::DrawAbout()
  241. {    RgnHandle        theRgn;
  242.     Handle            item;
  243.     Rect                bounds, clippedRect;
  244.     PicHandle        picH;
  245.     short                itemType;
  246.     DialogPtr        theDlog;
  247.     
  248.     theDlog = dp;
  249.     if (fClipRgn == NIL)
  250.         return;
  251.     theRgn = fClipRgn;
  252.     GetClip(theRgn);
  253.     GetDItem(theDlog, kB1 + lastItem, &itemType, &item, &bounds);
  254.     ClipRect(&bounds);
  255.     if(fAboutPic){
  256.         picH = fAboutPic;
  257.         clippedRect = (**fAboutPic).picFrame;
  258.         SetRect(&clippedRect, bounds.left, bounds.top, bounds.left + ((**fAboutPic).picFrame.right - (**fAboutPic).picFrame.left), bounds.top + ((**fAboutPic).picFrame.bottom - (**fAboutPic).picFrame.top));
  259.         OffsetRect(&clippedRect, 0, -fScrolled);
  260.         DrawPicture(picH, &clippedRect);
  261.         if (!fAbout)
  262.             ValidRect(&bounds);
  263.     }
  264.     SetClip(theRgn);
  265. }
  266.  
  267. void    PixelFlipper::Update()
  268. {    short                itemType, oldFont, oldSize, bits;
  269.     Handle            item;
  270.     Rect                bounds, clippedRect;
  271.     Str255            str, str2;
  272.     GrafPtr            port;
  273.     GDHandle        curGDev;
  274.     Boolean            mode;
  275.     DialogPtr        theDlog;
  276.     VersRecHdl    v;
  277.     CIconHandle    cicn;
  278.     
  279.     theDlog = dp;
  280.     GetPort(&port);
  281.     oldFont = port->txFont;
  282.     oldSize = port->txSize;
  283.     TextFont(geneva);
  284.     TextSize(9);
  285.     DrawAbout();
  286.     if (cicn = GetCIcon(kICNGood)){
  287.         GetDItem(theDlog, kCICN + lastItem, &itemType, &item, &bounds);
  288.         HLock (cicn);
  289.         PlotCIcon(&bounds, cicn);
  290.         HUnlock(cicn);
  291.         DisposCIcon(cicn);
  292.     }
  293.     GetDItem(theDlog, kB2 + lastItem, &itemType, &item, &bounds);
  294.     if (fLoaded == kNotLoaded)
  295.         GetIndString(str, kStrErr, kNotLoaded);
  296.     else{
  297.         curGDev = GetGDevice();            /* always update the graphics device global */
  298.         mode = ((**curGDev).gdFlags & 1);
  299.         bits    =(**(**curGDev).gdPMap).pixelSize;
  300.         NumToString((long)bits, str);
  301.         GetIndString(str2, kStrPixel, kBits);
  302.         pStrcat(str, str2);
  303.         GetIndString(str2, kStrPixel, kComma);
  304.         pStrcat(str, str2);
  305.         itemType = mode ? kColorS : kGrayS;        
  306.         GetIndString(str2, kStrPixel, itemType);
  307.         pStrcat(str, str2);
  308.         if(v = (VersRecHdl)Get1Resource('vers', 1)){
  309.             GetIndString(str2, kStrPixel, kComma);
  310.             pStrcat(str, str2);
  311.             bits = 0;
  312.             switch ((**v).stage){
  313.                 case development:
  314.                     bits = kDev;
  315.                     break;
  316.                 case alpha:
  317.                     bits = kAlpha;
  318.                     break;
  319.                 case beta:
  320.                     bits = kBeta;
  321.                     break;
  322.                 case final:
  323.                     bits = kfinal;
  324.                     break;
  325.             }
  326.             if (bits != 0){
  327.                 GetIndString(str2, kStrPixel, bits);
  328.                 pStrcat(str, str2);
  329.                 pStrcat(str, (**v).message);
  330.                 ReleaseResource(v);
  331.             }
  332.         }    
  333.     }
  334.     TextBox(&str[1], (long)str[0], &bounds, teJustLeft);
  335.     ValidRect(&bounds);
  336.     TextFont(oldFont);
  337.     TextSize(oldSize);
  338.     SetPort(port);
  339. }
  340.  
  341. void    PixelFlipper::ClearPrefsCtl()
  342. {
  343.     SetDCtl(kChanges, FALSE);            
  344.     SetDCtl(kShowIcon, FALSE);            
  345.     SetDCtl(kControl, FALSE);            
  346.     SetDCtl(kOption, FALSE);            
  347.     SetDCtl( kCapsLock, FALSE);            
  348.     SetDCtl(kShift, FALSE);            
  349.     SetDCtl(kCommand, FALSE);
  350.     SetDCtl(kActive, FALSE);            
  351.     SetDCtl(kInactive, FALSE);
  352. }
  353.  
  354. void PixelFlipper::SetPrefsCtl()
  355. {
  356.     ClearPrefsCtl();
  357.     if (fPrefs->isPerm)
  358.         SetDCtl(kChanges, fPrefs->isPerm);            
  359.     if (fPrefs->showIcon)
  360.         SetDCtl(kShowIcon, fPrefs->showIcon);            
  361.     if (fPrefs->hotKey & controlKey)    /* controlKey    */
  362.         SetDCtl(kControl, 1);            
  363.     if (fPrefs->hotKey & optionKey)    /* optionKey    */
  364.         SetDCtl(kOption, 1);            
  365.     if (fPrefs->hotKey & alphaLock)    /* cap lock        */
  366.         SetDCtl(kCapsLock, 1);            
  367.     if (fPrefs->hotKey &  shiftKey)    /* shiftKey        */
  368.         SetDCtl(kShift, 1);            
  369.     if (fPrefs->hotKey & cmdKey)            /* cmdKey            */
  370.         SetDCtl(kCommand, 1);        
  371.     if (fPrefs->active)                            /* is INIT active            */
  372.         SetDCtl(kActive, 1);
  373.     else
  374.         SetDCtl(kInactive, 1);            
  375. }
  376.